package com.magicode.cms.service.impl;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import javax.transaction.Transactional;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.magicode.cms.entiy.base.EventLive;
import com.magicode.cms.entiy.base.News;
import com.magicode.cms.entiy.base.Picture;
import com.magicode.cms.entiy.base.User;
import com.magicode.cms.service.IImageService;
import com.magicode.common.assist.PropertiesUtil;
import com.magicode.common.dao.ICommonDaoService;
import com.magicode.common.exception.BusinessException;
import com.magicode.common.image.AverageImageScale;
import com.magicode.common.image.ImageScale;
import com.magicode.common.javabean.UeImageBean;
import com.magicode.common.upload.FileRepository;
import com.magicode.common.upload.UploadUtils;

/**
 * 图片上传服务类
 * @author xwq
 *
 */
@Service("imageService")
@Transactional
public class ImageServiceImpl implements IImageService {

	@Autowired
	@Qualifier("commonDaoService")
	private ICommonDaoService commonDaoService;
	
	@Autowired
	@Qualifier("fileRepository")
	private FileRepository fileRepository;
	
	@Autowired
	@Qualifier("imageScaleImpl")
	private ImageScale imageScaleimpl;
	
	@Autowired
	@Qualifier("ueImageBean")
	private UeImageBean ueImageBean;
	
	@Override
	public News saveImage(News news,MultipartFile file, ServletContext ctx) throws BusinessException {
		Picture picture =saveImage(file,  ctx);
		news.setMainPic(picture);
		return news;
	}
	/**
	 * 将多个附件保存至对象的指定属性中
	 * @throws Exception 
	 */
	@Override
	public Object saveImages(Object obj , String propertyName,MultipartFile[] files, ServletContext ctx)	 throws Exception {
		 List<Picture> list = new ArrayList();
		BeanWrapperImpl bw = new BeanWrapperImpl( obj );
		 list = saveImages(files,  ctx);
		 List<Picture> oldPicList = null;
		 String oldPicSql = "select t."+propertyName+" from  "+obj.getClass().getName()+" t where t.id = :id ";
		 Map paraMap = new HashMap();
//		 获取该对象的ID属性；
		 paraMap.put("id", bw.getPropertyValue("id"));
		 oldPicList = commonDaoService.queryList(oldPicSql, paraMap);
		 if(oldPicList == null){
			 oldPicList = new ArrayList();
		 }
		 if(list != null){
			 oldPicList.addAll(list);
		 }
//			如果该对象不为空且属性存在，设置图片
			if(obj != null && propertyName != null && oldPicList != null){
					bw.setPropertyValue(propertyName,oldPicList);
			}
		 return obj;
	}
	
	/**
	 * 将多个附件保存至对象的指定属性中
	 * @throws Exception 
	 */
	@Override
	public Object saveImages(Object obj , String propertyName,MultipartFile[] files, ServletContext ctx,float scale)	 throws Exception {
		 List<Picture> list = new ArrayList();
		BeanWrapperImpl bw = new BeanWrapperImpl( obj );
		 list = saveImages(files,  ctx, scale);
		 List<Picture> oldPicList = null;
		 String oldPicSql = "select t."+propertyName+" from  "+obj.getClass().getName()+" t where t.id = :id ";
		 Map paraMap = new HashMap();
//		 获取该对象的ID属性；
		 paraMap.put("id", bw.getPropertyValue("id"));
		 oldPicList = commonDaoService.queryList(oldPicSql, paraMap);
		 if(oldPicList == null){
			 oldPicList = new ArrayList();
		 }
		 if(list != null){
			 oldPicList.addAll(list);
		 }
//			如果该对象不为空且属性存在，设置图片
			if(obj != null && propertyName != null && oldPicList != null){
					bw.setPropertyValue(propertyName,oldPicList);
			}
		 return obj;
	}
	
	/**
	 * 将多个附件保存至对象的指定属性中
	 * @throws Exception 
	 */
	@Override
	public Object saveImages(Object obj , String propertyName,MultipartFile[] files, ServletContext ctx,String width, String height)	 throws Exception {
		 List<Picture> list = new ArrayList();
		BeanWrapperImpl bw = new BeanWrapperImpl( obj );
		 list = saveImages(files,  ctx, width, height);
		 List<Picture> oldPicList = null;
		 String oldPicSql = "select t."+propertyName+" from  "+obj.getClass().getName()+" t where t.id = :id ";
		 Map paraMap = new HashMap();
//		 获取该对象的ID属性；
		 paraMap.put("id", bw.getPropertyValue("id"));
		 oldPicList = commonDaoService.queryList(oldPicSql, paraMap);
		 if(oldPicList == null){
			 oldPicList = new ArrayList();
		 }
		 if(list != null){
			 oldPicList.addAll(list);
		 }
//			如果该对象不为空且属性存在，设置图片
			if(obj != null && propertyName != null && oldPicList != null){
					bw.setPropertyValue(propertyName,oldPicList);
			}
		 return obj;
	}
	/**
	 * 将附件保存至对象的指定属性中
	 */
	@Override
	public Object saveImage(Object obj , String propertyName,MultipartFile file, ServletContext ctx) throws BusinessException {
		if(isSizeLargerThanProp(file))
			throw new BusinessException("上传图片大小不能超过2M");
		
		Picture picture =saveImage( file,  ctx);
//		如果该对象不为空且属性存在，设置图片
		if(obj != null && propertyName != null ){
			BeanWrapperImpl bw = new BeanWrapperImpl( obj );
				bw.setPropertyValue(propertyName,picture);
		}
		return obj;
	}
	/**
	 * 将附件保存至对象的指定属性中(根据比例缩放)
	 */
	@Override
	public Object saveImage(Object obj , String propertyName,MultipartFile file, ServletContext ctx, float scale) throws BusinessException {
		if(isSizeLargerThanProp(file))
			throw new BusinessException("上传图片大小不能超过2M");
		
		Picture picture =saveImage( file,  ctx, scale);
//		如果该对象不为空且属性存在，设置图片
		if(obj != null && propertyName != null ){
			BeanWrapperImpl bw = new BeanWrapperImpl( obj );
				bw.setPropertyValue(propertyName,picture);
		}
		return obj;
	}
	
	/**
	 * 将附件保存至对象的指定属性中(根据指定大小缩放)
	 */
	@Override
	public Object saveImage(Object obj , String propertyName,MultipartFile file, ServletContext ctx, String width, String height) throws BusinessException {
		if(isSizeLargerThanProp(file))
			throw new BusinessException("上传图片大小不能超过2M");
		
		Picture picture =saveImage( file,  ctx, width, height);
//		如果该对象不为空且属性存在，设置图片
		if(obj != null && propertyName != null ){
			BeanWrapperImpl bw = new BeanWrapperImpl( obj );
				bw.setPropertyValue(propertyName,picture);
		}
		return obj;
	}
	
	/**
	 * 将附件保存至数据库中
	 * 返回Picture对象；
	 * @param file
	 * @param ctx
	 * @return
	 * @throws BusinessException
	 */
	private Picture saveImage(MultipartFile file, ServletContext ctx,String width, String height) throws BusinessException{
		if(isSizeLargerThanProp(file))
			throw new BusinessException("上传图片大小不能超过2M");
		String images_upload_path = PropertiesUtil.parseStr("images_upload_path");
		Map map = saveNormalImage(file, ctx, images_upload_path);
		String filename = (String) map.get("filename");
		File destFile = (File) map.get("file");
		int [] wh = getSize(width, height, destFile);//根据缩放比例获取宽高
		String flname = null;
		if(null != wh && wh.length > 1){
			flname = resizeimage(destFile, ctx, images_upload_path,String.valueOf(wh[0]),String.valueOf(wh[1])); 
		}else{
			flname = resizeimage(destFile, ctx, images_upload_path,null,null);
		}
		Picture picture = new Picture();
		picture.setNormalPath(filename.replaceAll(images_upload_path, ""));
		picture.setMinPath(flname.replaceAll(images_upload_path, ""));
		picture.setOrginFilename(destFile.getName());
		picture.setFilename(destFile.getName());
		picture =  (Picture) this.commonDaoService.merge(picture);
		return picture;
	}
	/**
	 * 将附件保存至数据库中
	 * 返回Picture对象；
	 * @param file
	 * @param ctx
	 * @return
	 * @throws BusinessException
	 */
	private Picture saveImage(MultipartFile file, ServletContext ctx,float scale) throws BusinessException{
		if(isSizeLargerThanProp(file))
			throw new BusinessException("上传图片大小不能超过2M");
		String images_upload_path = PropertiesUtil.parseStr("images_upload_path");
		Map map = saveNormalImage(file, ctx, images_upload_path);
		String filename = (String) map.get("filename");
		File destFile = (File) map.get("file");
		int [] wh = getSize(scale, destFile);//根据缩放比例获取宽高
		String flname = null;
		if(null != wh && wh.length > 1){
			flname = resizeimage(destFile, ctx, images_upload_path,String.valueOf(wh[0]),String.valueOf(wh[1])); 
		}else{
			flname = resizeimage(destFile, ctx, images_upload_path,null,null);
		}
		Picture picture = new Picture();
		picture.setNormalPath(filename.replaceAll(images_upload_path, ""));
		picture.setMinPath(flname.replaceAll(images_upload_path, ""));
		picture.setOrginFilename(destFile.getName());
		picture.setFilename(destFile.getName());
		picture =  (Picture) this.commonDaoService.merge(picture);
		return picture;
	}
	/**
	 * 将附件保存至数据库中
	 * 返回Picture对象；
	 * @param file
	 * @param ctx
	 * @return
	 * @throws BusinessException
	 */
	private Picture saveImage(MultipartFile file, ServletContext ctx) throws BusinessException{
		if(isSizeLargerThanProp(file))
			throw new BusinessException("上传图片大小不能超过2M");
		String images_upload_path = PropertiesUtil.parseStr("images_upload_path");
		Map map = saveNormalImage(file, ctx, images_upload_path);
		String filename = (String) map.get("filename");
		File destFile = (File) map.get("file");
		String flname = resizeimage(destFile, ctx, images_upload_path, null, null);
		Picture picture = new Picture();
		picture.setNormalPath(filename.replaceAll(images_upload_path, ""));
		picture.setMinPath(flname.replaceAll(images_upload_path, ""));
		picture.setOrginFilename(destFile.getName());
		picture.setFilename(destFile.getName());
		picture =  (Picture) this.commonDaoService.merge(picture);
		return picture;
	}
	/**
	 * 保存多个附件，返回List<Picture>对象；
	 * @param files
	 * @param ctx
	 * @return
	 * @throws BusinessException
	 */
	private List<Picture> saveImages(MultipartFile[] files, ServletContext ctx,float scale) throws BusinessException{
		List<Picture> results = new ArrayList();
		if(files != null){
			for(MultipartFile file : files){
				results.add(saveImage(file , ctx,scale));
			}
		}
		return results;
	}
	
	/**
	 * 保存多个附件，返回List<Picture>对象；
	 * @param files
	 * @param ctx
	 * @return
	 * @throws BusinessException
	 */
	private List<Picture> saveImages(MultipartFile[] files, ServletContext ctx,String width, String height) throws BusinessException{
		List<Picture> results = new ArrayList();
		if(files != null){
			for(MultipartFile file : files){
				results.add(saveImage(file , ctx,width,height));
			}
		}
		return results;
	}
	/**
	 * 保存多个附件，返回List<Picture>对象；
	 * @param files
	 * @param ctx
	 * @return
	 * @throws BusinessException
	 */
	private List<Picture> saveImages(MultipartFile[] files, ServletContext ctx) throws BusinessException{
		List<Picture> results = new ArrayList();
		if(files != null){
			for(MultipartFile file : files){
				results.add(saveImage(file , ctx));
			}
		}
		return results;
	}
	@Override
	public Map saveNormalImage(MultipartFile file, ServletContext ctx, String images_upload_path) throws BusinessException {
		Map map = new HashMap();
	    String origName = file.getOriginalFilename();
	    String ext = FilenameUtils.getExtension(origName).toLowerCase(
				Locale.ENGLISH);
	    String images_path_Prefix = PropertiesUtil.parseStr("images_path_Prefix");
		try {
			//原文件名
			map.put("orginFilename", origName);
			String filename = UploadUtils.generateFilename(images_upload_path, ext);
			//保存文件名，用于存储
			map.put("filename", filename);
			String realpath = "";
			//如果配有Prefix目录，以此为准，否则保存到tomcat webapp 项目根目录下
			if(images_path_Prefix!=null && images_path_Prefix.length()>0){
				realpath = images_path_Prefix + filename;
			}else{
				realpath=ctx.getRealPath(filename);
				if(realpath==null){
					realpath=ctx.getRealPath("/")+filename;
				}
			}
			File destFile = new File(realpath);
			destFile = UploadUtils.getUniqueFile(destFile);
			fileRepository.store(file, destFile);
			map.put("file", destFile);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 删除文件
	 * @param pic
	 * @return
	 * @throws BusinessException
	 */
	@Override
	public boolean delteImage(Picture pic,ServletContext ctx)  {
		boolean result = true;
//		realpath
	    String images_path_Prefix = PropertiesUtil.parseStr("images_path_Prefix");
		try {
//			删除大图
			{
			String filename =pic.getNormalPath();
			String realpath = "";
			//如果配有Prefix目录，以此为准，否则保存到tomcat webapp 项目根目录下
			if(images_path_Prefix!=null && images_path_Prefix.length()>0){
				realpath = images_path_Prefix + filename;
			}else{
				realpath=ctx.getRealPath(filename);
				if(realpath==null){
					realpath=ctx.getRealPath("/")+filename;
				}
			}
			File destFile = new File(realpath);
			destFile.delete();
			}
//			删除小图
			{
				String filename =pic.getMinPath();
				String realpath = "";
				//如果配有Prefix目录，以此为准，否则保存到tomcat webapp 项目根目录下
				if(images_path_Prefix!=null && images_path_Prefix.length()>0){
					realpath = images_path_Prefix + filename;
				}else{
					realpath=ctx.getRealPath(filename);
					if(realpath==null){
						realpath=ctx.getRealPath("/")+filename;
					}
				}
				File destFile = new File(realpath);
				destFile.delete();
				}
			this.commonDaoService.delete(pic);
			
		} catch (Exception e) {
			result = false;
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * 删除文件
	 * @param pic
	 * @return
	 * @throws BusinessException
	 */
	@Override
	public boolean delteImage(String picId,ServletContext ctx)  {
		Picture picture = (Picture) this.commonDaoService.get(Picture.class, picId);
		return delteImage(picture,ctx);
	}
	@Override
	public String resizeimage(File file, ServletContext ctx, String images_upload_path, String width, String height)
			throws BusinessException {
		String name = file.getName();
	    String ext = FilenameUtils.getExtension(name).toLowerCase(
				Locale.ENGLISH);
	    String origName = name.substring(0, name.lastIndexOf("."));
	    if(width==null){
	    	width = PropertiesUtil.parseStr("images_resize_width");
	    }
	    if(height==null){
	    	height = PropertiesUtil.parseStr("images_resize_height");
	    }
	    String images_path_Prefix = PropertiesUtil.parseStr("images_path_Prefix");
		try {
			String flname = origName+"_"+width+"_"+height;
			flname = UploadUtils.generateByFilename(images_upload_path, flname, ext);
			String realpath = "";
			//如果配有Prefix目录，以此为准，否则保存到tomcat webapp 项目根目录下
			if(images_path_Prefix!=null && images_path_Prefix.length()>0){
				realpath = images_path_Prefix + flname;
			}else{
				realpath=ctx.getRealPath(flname);
				if(realpath==null){
					realpath=ctx.getRealPath("/")+flname;
				}
			}
			File fileless = new File(realpath);
			imageScaleimpl.resizeFix(file, fileless, Integer.parseInt(width), Integer.parseInt(height));
			return flname;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
     * <b>function:</b> 通过指定的比例和图片对象，返回一个放大或缩小的宽度、高度
     * @author hoojo
     * @createDate 2012-2-7 上午10:27:59
     * @param scale 缩放比例
     * @param image 图片对象
     * @return 返回宽度、高度
     */
    private  int[] getSize(float scale, File file) {
    	BufferedImage srcImgBuff = null;
		try{
		srcImgBuff = ImageIO.read(file);
		int targetWidth = srcImgBuff.getWidth();
        int targetHeight = srcImgBuff.getHeight();
        long standardWidth = (Math.round(targetWidth * scale) > 335) ? 335 : targetWidth;
        long standardHeight =(Math.round(targetHeight * scale) > 460) ? 460 : targetHeight;
        return new int[] { Integer.parseInt(Long.toString(standardWidth)), Integer.parseInt(String.valueOf(standardHeight)) };
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("Image cannot read :"+file.getAbsolutePath());
		}
		return null;
    }
    /**
     * <b>function:</b> 通过指定的宽高和图片对象，返回一个放大或缩小的宽度、高度
     * @author hoojo
     * @createDate 2012-2-7 上午10:27:59
     * @param width 宽
     * @param hight 高
     * @param file 图片对象
     * @return 返回宽度、高度
     */
    private  int[] getSize(String width, String height, File file) {
    	BufferedImage srcImgBuff = null;
		try{
		srcImgBuff = ImageIO.read(file);
		int targetWidth = srcImgBuff.getWidth();//真实宽度
		int targetHeight = srcImgBuff.getHeight();//真实高度
		if(width==null){
			//以高度为基准进行缩放
	    	width = Integer.valueOf(srcImgBuff.getWidth() * (Integer.parseInt(height.trim())) / srcImgBuff.getHeight()).toString();
	    }
	    if(height==null){
	    	//以宽度为基准进行缩放
	    	height = Integer.valueOf(srcImgBuff.getHeight() * (Integer.parseInt(width.trim())) / srcImgBuff.getWidth()).toString();
	    }
        int standardWidth = (targetWidth > (Integer.parseInt(width.trim()))) ? Integer.parseInt(width.trim()) : targetWidth;
        int standardHeight =(targetHeight > (Integer.parseInt(height.trim()))) ? Integer.parseInt(height.trim()) : targetHeight;
        return new int[] { standardWidth, standardHeight };
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("Image cannot read :"+file.getAbsolutePath());
		}
		return null;
    }
	//判断文件是否大于配置参数
	private Boolean isSizeLargerThanProp(MultipartFile file){
		Integer image_size = 0;
		try {
			String size = PropertiesUtil.parseStr("image_size");
			image_size = Integer.parseInt(size);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 判断图片大小是否大于2M
	    if (file.getSize() > image_size) {
			return true;
	    }
	    return false;
	}

	@Override
	public UeImageBean saveUeImage(MultipartFile file, ServletContext ctx) {
		if(isSizeLargerThanProp(file))
			return null;
		String images_upload_path = PropertiesUtil.parseStr("images_upload_path");
		//为ue上传增加特殊目录
		images_upload_path += "/ue";
		String images_path_Prefix = PropertiesUtil.parseStr("images_path_Prefix");
		Long size = file.getSize();
		//原文件名
	    String origName = file.getOriginalFilename();
	    String ext = FilenameUtils.getExtension(origName).toLowerCase(
				Locale.ENGLISH);
		try {
			String filename = UploadUtils.generateFilename(images_upload_path, ext);
			String realpath = "";
			//如果配有Prefix目录，以此为准，否则保存到tomcat webapp 项目根目录下
			if(images_path_Prefix!=null && images_path_Prefix.length()>0){
				realpath = images_path_Prefix + filename;
			}else{
				realpath=ctx.getRealPath(filename);
				if(realpath==null){
					realpath=ctx.getRealPath("/")+filename;
				}
			}
			File destFile = new File(realpath);
			destFile = UploadUtils.getUniqueFile(destFile);
			fileRepository.store(file, destFile);
			ueImageBean = new UeImageBean();
			ueImageBean.setOriginal(origName);
			ueImageBean.setSize(size.toString());
			ueImageBean.setTitle(destFile.getName());
			ueImageBean.setUrl("/"+filename);
			ueImageBean.setType("."+ext);
			return ueImageBean;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public User saveHeadImage(User user, MultipartFile file, ServletContext ctx,  int maxWidth, int maxHeight, int cutTop, int cutLeft, int cutWidth,
			int cutHeight) throws BusinessException {
		if(isSizeLargerThanProp(file))
			throw new BusinessException("上传图片大小不能超过2M");
		String images_upload_path = PropertiesUtil.parseStr("images_upload_path");
		Map map = cutImage(file, ctx, images_upload_path, maxWidth, maxHeight, cutTop, cutLeft, cutWidth, cutHeight);
		String filename = (String) map.get("filename");
		File destFile = (File) map.get("file");
		String flname = resizeimage(destFile, ctx, images_upload_path + "/hd", "100", "100");
		Picture picture = new Picture();
		picture.setOrginFilename((String) map.get("orginFilename"));
		picture.setNormalPath(filename.replaceAll(images_upload_path, ""));
		picture.setMinPath(flname.replaceAll(images_upload_path, ""));
		picture.setFilename(destFile.getName());
		picture =  (Picture) this.commonDaoService.merge(picture);
		user.setMainPic(picture);
		user =  (User) this.commonDaoService.merge(user);
		return user;
	}
	/**
	 * 根据文件流保存图片
	 * 不执行图片裁剪
	 */
	@Override
	public Picture saveHeadImage(InputStream in, ServletContext ctx) throws BusinessException {
		String images_upload_path = PropertiesUtil.parseStr("images_upload_path");
		Map map = null;
//		格式：/hd/201505/
		String filePath = images_upload_path+ File.separator+"hd";
		try {
//			取中间头像
			System.out.println("Begin Resize!");
			map = AverageImageScale.resizeCenterFix(in, filePath, filePath, 100, 100);
//			map = AverageImageScale.resizeFix(in, filePath, filePath, 100, 100);
			
			
			System.out.println("End Resize!");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		图片名称
		String filename = (String) map.get("filename");
//		缩略图文件名
		String flname = (String)map.get("miniFilename");
		File srcFile = (File) map.get("file");
//		AverageImageScale
		Picture picture = new Picture();
		picture.setOrginFilename((String) map.get("orginFilename"));
		picture.setNormalPath(filename.replaceAll(images_upload_path, ""));
		picture.setMinPath(flname.replaceAll(images_upload_path, ""));
		picture.setFilename(srcFile.getName());
		picture =  (Picture) this.commonDaoService.merge(picture);		
		return picture;
	}
	@Override
	public Map cutImage(MultipartFile file, ServletContext ctx,
			String images_upload_path, int maxWidth, int maxHeight, int cutTop, int cutLeft, int cutWidth,
			int cutHeight) throws BusinessException {
		Map map = new HashMap();
	    String origName = file.getOriginalFilename();
	    String ext = FilenameUtils.getExtension(origName).toLowerCase(
				Locale.ENGLISH);
	    //为头像上传增加特殊目录
	  	images_upload_path += "/hd";
	    String images_path_Prefix = PropertiesUtil.parseStr("images_path_Prefix");
	    File orginFile = null;
		try {
			//原文件名
			map.put("orginFilename", origName);
			String filename = UploadUtils.generateFilename(images_upload_path, ext);
			//保存文件名，用于存储
			map.put("filename", filename);
			String realpath = "";
			//如果配有Prefix目录，以此为准，否则保存到tomcat webapp 项目根目录下
			if(images_path_Prefix!=null && images_path_Prefix.length()>0){
				realpath = images_path_Prefix + filename;
			}else{
				realpath=ctx.getRealPath(filename);
				if(realpath==null){
					realpath=ctx.getRealPath("/")+filename;
				}
			}
			orginFile = File.createTempFile("temp","."+ ext);
			file.transferTo(orginFile);
			File destFile = new File(realpath);
			imageScaleimpl.resizeFix(orginFile, destFile, maxWidth, maxHeight, cutTop, cutLeft, cutWidth, cutHeight);
			map.put("file", destFile);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			if(orginFile!=null)
				orginFile.delete();
		}
		return null;
	}
	/**
	 * String images_upload_path = PropertiesUtil.parseStr("images_upload_path");
		Map map = saveNormalImage(file, ctx, images_upload_path);
		String filename = (String) map.get("filename");
		File destFile = (File) map.get("file");
		String flname = resizeimage(destFile, ctx, images_upload_path, null, null);
		Picture picture = new Picture();
		picture.setNormalPath(filename.replaceAll(images_upload_path, ""));
		picture.setMinPath(flname.replaceAll(images_upload_path, ""));
		picture.setOrginFilename(destFile.getName());
		picture.setFilename(destFile.getName());
		picture =  (Picture) this.commonDaoService.merge(picture);
	 * @throws BusinessException 
	 */
	@Override
	public boolean resizeImage(Picture pic, ServletContext ctx) throws BusinessException {
		// TODO Auto-generated method stub
		try{
			String images_path_Prefix = PropertiesUtil.parseStr("images_path_Prefix");
			String images_upload_path = PropertiesUtil.parseStr("images_upload_path");
			String path = images_path_Prefix +images_upload_path+pic.getNormalPath();
			File destFile = new File(path);
			String minPath = resizeimage(destFile, ctx, images_upload_path, null, null);
			pic.setMinPath(minPath.replaceAll(images_upload_path, ""));
//			如果无法发生错误，设置文件Title
		}catch(Exception e){
			pic.setTitle("Can't read input file!");
			this.commonDaoService.update(pic);
		}
		try{
			pic.setSubject("updated");
			this.commonDaoService.update(pic);
		}catch(Exception e){
			throw new BusinessException(e);
		}
		
		return false;
	}
	
	@Override
	public boolean resizeImage(Picture pic, ServletContext ctx, float scale) throws BusinessException {
		// TODO Auto-generated method stub
		try{
			String images_path_Prefix = PropertiesUtil.parseStr("images_path_Prefix");
			String images_upload_path = PropertiesUtil.parseStr("images_upload_path");
			String path = images_path_Prefix +images_upload_path+pic.getNormalPath();
			File destFile = new File(path);
			int [] wh = getSize(scale, destFile);//根据缩放比例获取宽高
			String flname = null;
			if(null != wh && wh.length > 1){
				flname = resizeimage(destFile, ctx, images_upload_path,String.valueOf(wh[0]),String.valueOf(wh[1])); 
			}else{
				flname = resizeimage(destFile, ctx, images_upload_path,null,null);
			}
			pic.setMinPath(flname.replaceAll(images_upload_path, ""));
//			如果无法发生错误，设置文件Title
		}catch(Exception e){
			pic.setTitle("Can't read input file!");
			this.commonDaoService.update(pic);
		}
		try{
			pic.setSubject("updated");
			this.commonDaoService.update(pic);
		}catch(Exception e){
			throw new BusinessException(e);
		}
		
		return false;
	}
	
	@Override
	public boolean resizeImage(Picture pic, ServletContext ctx,String width, String height) throws BusinessException {
		// TODO Auto-generated method stub
		try{
			String images_path_Prefix = PropertiesUtil.parseStr("images_path_Prefix");
			String images_upload_path = PropertiesUtil.parseStr("images_upload_path");
			String path = images_path_Prefix +images_upload_path+pic.getNormalPath();
			File destFile = new File(path);
			int [] wh = getSize(width,height, destFile);//根据缩放比例获取宽高
			String flname = null;
			if(null != wh && wh.length > 1){
				flname = resizeimage(destFile, ctx, images_upload_path,String.valueOf(wh[0]),String.valueOf(wh[1])); 
			}else{
				flname = resizeimage(destFile, ctx, images_upload_path,null,null);
			}
			pic.setMinPath(flname.replaceAll(images_upload_path, ""));
//			如果无法发生错误，设置文件Title
		}catch(Exception e){
			pic.setTitle("Can't read input file!");
			this.commonDaoService.update(pic);
		}
		try{
			pic.setSubject("updated");
			this.commonDaoService.update(pic);
		}catch(Exception e){
			throw new BusinessException(e);
		}
		
		return false;
	}
	@Override
	public EventLive saveImage(EventLive eventLive, MultipartFile file,
			ServletContext ctx) throws BusinessException {
		// TODO Auto-generated method stub
		return null;
	}
}
